﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors;
using MINERP.InfoConfig.Controller;
using MINERP.MODEL.InfoConfig;
using MINERP.DAL;
using MINERP.COM;

namespace MINERP.InfoConfig
{
    public partial class FrmTechManager : FrmMDIChildFormTemplate
    {
        private TechController      _controller =  new TechController();
        private tb_Tech             _tech = null;
        private static FrmTechManager instance = null;
        private FrmTechManager()
        {
            InitializeComponent();
        }

        public static FrmTechManager CreateInstance
        {
            get
            {
                if (instance == null)
                {
                    instance = new FrmTechManager();
                }
                return instance;
            }
        }

        private void FrmTechManager_Load(object sender, EventArgs e)
        {
            _controller.Grid = gridControlTech;
            _controller.View = gridViewTech;
            _controller.GetTechData();
        }

        private void gridViewTech_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            _tech = (tb_Tech)gridViewTech.GetFocusedRow();
            if (_tech != null)
            {
                switch (Convert.ToInt32(_tech.TechType))
                {
                    case 0:
                        cmbTechType.Text = "系统节点";
                        break;
                    case 1:
                        cmbTechType.Text = "实物操作节点";
                        break;
                    case 2:
                        cmbTechType.Text = "非实物操作节点";
                        break;
                    case 3:
                        cmbTechType.Text = "拼版打印";
                        break;
                    case 4:
                        cmbTechType.Text = "委外加工";
                        break;
                    case 5:
                        cmbTechType.Text = "理件分堆";
                        break;
                    case 6:
                        cmbTechType.Text = "处理多份(调色)";
                        break;
                    case 7:
                        cmbTechType.Text = "条码分片";
                        break;
                    case 8:
                        cmbTechType.Text = "扩印";
                        break;
                    case 9:
                        cmbTechType.Text = "生成Condition文件";
                        break;
                    case 10:
                        cmbTechType.Text = "打印管理";
                        break;
                    case 11:
                        cmbTechType.Text = "相册厂";
                        break;
                    case 12:
                        cmbTechType.Text = "看样";
                        break;
                    case 13:
                        cmbTechType.Text = "导出照片";
                        break;
                    case 14:
                        cmbTechType.Text = "外加工照片";
                        break;
                }
                txtTechName.Text = _tech.TechName;
                txtWokerOrder.Text = _tech.WokerOrder.ToString();
                txtDes.Text = _tech.Des;
                txtDirectoryName.Text = _tech.DirectoryName;
                txtNeedTime.Text = _tech.NeedTime.ToString();
                chkIsDesign.Checked = _tech.IsDesign;
                chkIsNecessary.Checked = (_tech.IsNecessary == null) ? false : (bool)_tech.IsNecessary;
            }
        }
        // 添加
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (btnAdd.Text != "保存")
            {
                PrepareAdd(true);
                txtTechName.Focus();
                return;
            }
            if (CheckData())
            {
                tb_Tech tech = new tb_Tech();
                tech.TechName           = txtTechName.Text;     
                tech.WokerOrder         = Convert.ToInt32(txtWokerOrder.Text);    
                tech.Des                = txtDes.Text;    
                tech.DirectoryName      = txtDirectoryName.Text; 
                tech.NeedTime           = Convert.ToDouble(txtNeedTime.Text);
                tech.IsDesign           = chkIsDesign.Checked;
                tech.IsNecessary        = chkIsNecessary.Checked;
                tech.TechType = cmbTechType.SelectedIndex;
                //switch (cmbTechType.Text.Trim())
                //{
                //    case "开始":
                //        tech.TechType = 0;
                //        break;
                //    case "输出管理":
                //        tech.TechType = 3;
                //        break;
                //    case "修片设计打印管理":
                //        tech.TechType = 2;
                //        break;
                //    case "委外加工":
                //        tech.TechType = 4;
                //        break;
                //    case "条码分片":
                //        tech.TechType = 7;
                //        break;
                //    case "扩印":
                //        tech.TechType = 8;
                //        break;
                //    case "理件分堆":
                //        tech.TechType = 5;
                //        break;
                //    case "调色":
                //        tech.TechType = 6;
                //        break;
                //}
                _controller.Add(tech);

                //link datagrid
                _controller.Grid  = gridControlTech;
                _controller.View = gridViewTech;
                _controller.GetTechData();

                PrepareAdd(false);
                MessageBox.Show("添加成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        // 更新
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (btnUpdate.Text != "保存")
            {
                PrepareUpdate(true);
                txtTechName.Focus();
                return;
            }
            if (CheckData())
            {
                tb_Tech tech = _controller.GetTechObj(_tech.TechId);
                tech.TechName = txtTechName.Text;
                tech.WokerOrder = Convert.ToInt32(txtWokerOrder.Text);
                tech.Des = txtDes.Text;
                tech.DirectoryName = txtDirectoryName.Text;
                tech.NeedTime = Convert.ToDouble(txtNeedTime.Text);
                tech.IsDesign = chkIsDesign.Checked;
                tech.IsNecessary = chkIsNecessary.Checked;
                tech.TechType = cmbTechType.SelectedIndex;
                //switch (cmbTechType.Text.Trim())
                //{
                //    case "开始":
                //        tech.TechType = 0;
                //        break;
                //    case "输出管理":
                //        tech.TechType = 3;
                //        break;
                //    case "修片设计打印管理":
                //        tech.TechType = 2 ;
                //        break;
                //    case "委外加工":
                //        tech.TechType = 4;
                //        break;
                //    case "条码分片":
                //        tech.TechType = 7;
                //        break;
                //    case "扩印":
                //        tech.TechType = 8;
                //        break;
                //    case "理件分堆":
                //        tech.TechType = 5;
                //        break;
                //    case "调色":
                //        tech.TechType = 6;
                //        break;

                //}
                _controller.Update();

                //refresh datagrid
                _controller.Grid = gridControlTech;
                _controller.View = gridViewTech;
                _controller.GetTechData();
                PrepareUpdate(false);

                MessageBox.Show("更新成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            } 
        }
        //
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == MessageBox.Show("确认删除吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
            {
                tb_Tech tech = _controller.GetTechObj(_tech.TechId);
                _controller.Delete(tech);

                //refresh datagrid
                _controller.Grid = gridControlTech;
                _controller.View = gridViewTech;
                _controller.GetTechData();
            }
        }
        // 取消
        private void btnCancel_Click(object sender, EventArgs e)
        {
            txtTechName.Enabled = false;
            cmbTechType.Enabled = false;
            txtWokerOrder.Enabled = false;
            txtDes.Enabled = false;
            txtDirectoryName.Enabled = false;
            txtNeedTime.Enabled = false;
            chkIsDesign.Enabled = false;
            chkIsNecessary.Enabled = false;

            btnAdd.Text = "添加";
            btnUpdate.Text = "更新";
            btnAdd.Enabled = true;
            btnUpdate.Enabled = true;
            btnDelete.Enabled = true;
        }
        /// <summary>
        /// 检查数据完整性

        /// </summary>
        /// <returns></returns>
        private bool CheckData()
        {
            bool flag = true;
            if (txtTechName.Text == string.Empty)
            {
                txtTechName.Focus();
                MessageBox.Show("节点名称不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (cmbTechType.Text == string.Empty)
            {
                cmbTechType.Focus();
                MessageBox.Show("节点类型不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }

            if (txtWokerOrder.Text == string.Empty)
            {
                txtWokerOrder.Focus();
                MessageBox.Show("节点工序不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (txtDes.Text == string.Empty)
            {
                txtDes.Focus();
                MessageBox.Show("节点描述不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (txtDirectoryName.Text == string.Empty)
            {
                txtDirectoryName.Focus();
                MessageBox.Show("目录名称不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (txtNeedTime.Text == string.Empty)
            {
                txtNeedTime.Focus();
                MessageBox.Show("节点完成时间不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;

            }
            return flag;
        }
        /// <summary>
        /// 添加前控件设置

        /// </summary>
        /// <param name="bl"></param>
        private void PrepareAdd(bool bl)
        {   
            txtTechName.Enabled = bl;
            cmbTechType.Enabled = bl;
            txtWokerOrder.Enabled = bl;
            txtDes.Enabled = bl;
            txtDirectoryName.Enabled = bl;
            txtNeedTime.Enabled = bl;
            chkIsDesign.Enabled = bl;
            chkIsNecessary.Enabled = bl;

            txtTechName.Text     = string.Empty;
            cmbTechType.Text     = string.Empty;
            txtWokerOrder.Text   = string.Empty;
            txtDes.Text          = string.Empty;
            txtDirectoryName.Text= string.Empty;
            txtNeedTime.Text     = string.Empty;
  

            if (bl)
            {
                btnAdd.Text = "保存";
                btnUpdate.Enabled = false;
                btnDelete.Enabled = false;
            }
            else
            {
                btnAdd.Text = "添加";
                btnUpdate.Enabled = true;
                btnDelete.Enabled = true;
            }
        }

        /// <summary>
        /// 更新前控件设置

        /// </summary>
        /// <param name="bl"></param>
        private void PrepareUpdate(bool bl)
        {
            txtTechName.Enabled = bl;
            cmbTechType.Enabled = bl;
            txtWokerOrder.Enabled = bl;
            txtDes.Enabled = bl;
            txtDirectoryName.Enabled = bl;
            txtNeedTime.Enabled = bl;
            chkIsDesign.Enabled = bl;
            chkIsNecessary.Enabled = bl;

            if (bl)
            {

                btnUpdate.Text = "保存";
                btnAdd.Enabled = false;
                btnDelete.Enabled = false;
            }
            else
            {
                btnUpdate.Text = "更新";
                btnAdd.Enabled = true;
                btnDelete.Enabled = true;
            }
        }

        private void FrmTechManager_FormClosed(object sender, FormClosedEventArgs e)
        {
            instance = null;
        }

        private void txtNeedTime_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                Convert.ToDouble(txtNeedTime.Text.Trim());
            }
            catch
            {
                MessageBox.Show("请输入数字", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtNeedTime.Text = string.Empty;
            }
        }

        private void txtWokerOrder_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                Convert.ToInt32(txtWokerOrder.Text.Trim());
            }
            catch
            {
                MessageBox.Show("请输入整数", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtWokerOrder.Text = string.Empty;
            }
        }

        private void groupControl1_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}
